package com.nl.controller.xuyi.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.xuyi.bean.XuYiPointInfo;
import com.nl.controller.xuyi.service.XuYiPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @desc 点位
 * @return null
 * @author ll
 * @date 2020/9/27 11:00
 */
@RestController
@RequestMapping("/xuYiPoint")
@Slf4j
public class XuYiPointController {
    @Autowired
    XuYiPointService xuYiPointService;
    @PostMapping("findPointNameList")
    public Response findPointNameList(
            @RequestParam(value = "regionNameText", required = false) String regionNameText,
            @RequestParam(value = "alreadyRelate", required = false) String alreadyRelate
    ) {
        PageInfo<XuYiPointInfo> pageInfo  = xuYiPointService.findPointNameList(1,100000,regionNameText,alreadyRelate);
        log.info("点位名称联想参数：{}", regionNameText,alreadyRelate);
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }

    @PostMapping("pointInfoAdd")
    public Response pointInfoAdd(
            @RequestParam(value = "pointName", required = false) String pointName,
            @RequestParam(value = "gpsLngLats", required = false) String gpsLngLats,
            @RequestParam(value = "remark", required = false) String remark,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {
        log.info("插入审核表参数：{}", paramMap.toString());
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String operatorTel = user.getMsisdn();
        int insertCheckResult = xuYiPointService.pointInfoAdd(pointName,gpsLngLats,remark,operatorTel);
        log.debug("插入变电站表返回参数：{}", insertCheckResult);
        if (insertCheckResult > 0) {
            return ResponseUtil.sucess("点位添加成功,待审核！");
        } else {
            log.debug("插入变电站表返回参数：{}", insertCheckResult);
            return ResponseUtil.error(insertCheckResult, "点位添加失败，请重试");
        }
    }

    @PostMapping("findPointNameList2")
    public Response findPointNameList(
            @RequestParam(value = "pointNameText", required = false) String pointNameText
    ) {
        System.out.println("---"+pointNameText);
        List<XuYiPointInfo> xuYiPointList  = xuYiPointService.findPointNameList2(pointNameText);
        log.info("点位名称联想参数：{}", pointNameText);
        return ResponseUtil.sucess(xuYiPointList);
    }

    @RequestMapping("findPointInfoList")
    public Response findPointInfoList(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "pointNameText",required=false) String pointNameText
            ){
        PageInfo<XuYiPointInfo> pageInfo = xuYiPointService.findPointInfoList(page,limit,pointNameText);
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }

    @PostMapping("findPointInfoByName")
    public Response findPointInfoByName(
            @RequestParam(value = "pointName", required = false) String pointName
    ) {
        XuYiPointInfo xuYiPointInfo = xuYiPointService.findPointInfoByName(pointName);
        log.info("根据名称查询点位信息参数：{}", pointName);
        log.info("根据名称查询点位信息返回：{}", xuYiPointInfo);
        return ResponseUtil.sucess(xuYiPointInfo);
    }

    @PostMapping("judgeIsRepeat")
    public Response judgeIsRepeat(
            @RequestParam(value = "pointName", required = false) String pointName
    ) {
        int count = xuYiPointService.judgeIsRepeat(pointName);
        log.info("根据名称查询点位信息参数：{}", pointName);
        log.info("根据名称查询点位信息返回：{}", count);
        return ResponseUtil.sucess(count);
    }


    @PostMapping("pointInfoModify")
    public Response pointInfoAdd(
            @RequestParam(value = "pointId", required = false) String pointId,
            @RequestParam(value = "pointName", required = false) String pointName,
            @RequestParam(value = "gpsLngLats", required = false) String gpsLngLats,
            @RequestParam(value = "remark", required = false) String remark,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {
        log.info("插入审核表参数：{}", paramMap.toString());
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String operatorTel = user.getMsisdn();
        int insertCheckResult = xuYiPointService.pointInfoModify(pointId,pointName,gpsLngLats,remark,operatorTel);
        log.debug("插入变电站表返回参数：{}", insertCheckResult);
        if (insertCheckResult > 0) {
            return ResponseUtil.sucess("点位添加成功,待审核！");
        } else {
            log.debug("插入变电站表返回参数：{}", insertCheckResult);
            return ResponseUtil.error(insertCheckResult, "点位添加失败，请重试");
        }
    }

    @PostMapping("deletePointInfo")
    private Response deletePointInfo(
            @RequestParam(value = "pointId") String pointId,
            @RequestParam(value = "pointName") String pointName,
            HttpSession session,
            @RequestParam Map<String, Object> paramMap) {

        log.info("删除点位信息插入审核表参数：{}", paramMap.toString());
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String operatorTel = user.getMsisdn();
        int result = xuYiPointService.deletePointInfo(pointId,pointName,operatorTel);
        log.info("插入变电站表返回参数：{}", result);
        if(result>0){
            log.info("删除点位信息:{}" ,paramMap.toString());
            log.info("删除点位信息：",result);
            return ResponseUtil.sucess("success");
        } else {
            log.error("删除点位信息：{}", result);
            return ResponseUtil.error(-1, "删除点位信息失败，请重试");
        }

    }

}
